package et.item.drill;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.BlockEvent;
import thaumcraft.api.aspects.Aspect;
import thaumcraft.common.config.ConfigItems;
import thaumcraft.common.items.wands.ItemWandCasting;

/**
 * Created by winston_wang on 2017/6/15.
 */
public class ETItemLandDrill extends ETItemDrill {
    public ETItemLandDrill(){
        super(8.0f, 80.0f, 57000, 100, 1, 8,17.0f);
        MinecraftForge.EVENT_BUS.register(this);
        setTextureName("et:itemVoidLandDrill");
        setUnlocalizedName("landVoidDrill");
        setHarvestLevel("axe",8);
    }
    @Override
    public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, Block block, int par4, int par5, int par6, EntityLivingBase par7EntityLiving) {
        if (!par2World.isRemote) {
            if (block.equals(Blocks.stone)
                    ||block.equals(Blocks.dirt)){
                par2World.setBlockToAir(par4,par5,par6);
                if (((EntityPlayer)par7EntityLiving).inventory.hasItem(ConfigItems.itemWandCasting)){
                    for(ItemStack itemStack : ((EntityPlayer)par7EntityLiving).inventory.mainInventory){
                        if (itemStack!=null
                                &&itemStack.getItem().equals(ConfigItems.itemWandCasting)){
                            ((ItemWandCasting)itemStack.getItem()).addRealVis(itemStack,Aspect.EARTH,25,true);
                        }
                    }
                }
            }
        }
        return super.onBlockDestroyed(par1ItemStack,par2World, block,par4,par5,par6,par7EntityLiving);
    }
    @SubscribeEvent
    public void onBlockDrop(BlockEvent.HarvestDropsEvent event){
        try {
            if (event.harvester != null
                    && event.harvester.getCurrentEquippedItem() != null
                    && event.harvester.getCurrentEquippedItem().getItem().equals(this)) {
                event.world.setBlockToAir(event.x, event.y, event.z);
                for (ItemStack i : event.drops) {
                    EntityItem entityItem = new EntityItem(event.world, event.harvester.posX, event.harvester.posY, event.harvester.posZ, i);
                    event.world.spawnEntityInWorld(entityItem);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
